package leetcode.simple.q461;

public class Main {
    public static void main(String[] args) {

        int i = hammingDistance2(1, 8);
        System.out.println(i);
    }

    /**
     * 我的题解
     * @param x
     * @param y
     * @return
     */
    public static int hammingDistance(int x, int y) {
        int i = x ^ y;
        String s = Integer.toBinaryString(i);
        int count = 0;
        for (int i1 = 0; i1 < s.length(); i1++) {
            if (s.charAt(i1) == '1'){
                count++;
            }
        }

        return count;
    }

    public static int hammingDistance2(int x, int y) {
        int xor = x ^ y;
        int count = 0;
        while (xor != 0){
            if (xor % 2 == 1){
                count++;
            }
            xor = xor >> 1;
        }

        return count;
    }
}

class Solution {
    /**
     * 官方题解1
     * @param x
     * @param y
     * @return
     */
    public int hammingDistance1(int x, int y) {
        int xor = x ^ y;
        int distance = 0;
        while (xor != 0) {
            if (xor % 2 == 1) {
                distance += 1;
            }
            xor = xor >> 1;
        }
        return distance;
    }
}
